package com.linxiao.call.controller;

import com.alibaba.fastjson2.JSONObject;
import com.linxiao.call.domain.CrmWebcallCdr;
import com.linxiao.call.service.ICrmWebcallCdrService;
import com.linxiao.call.vo.CrmWebcallCdrParamVO;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

/**
 * webCall语音通知Controller
 *
 * @author qiqi
 * @date 2022-06-10
 */
@RestController
@RequestMapping("/crm/webcall")
public class CrmWebcallCdrController extends BaseController {
    public Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private ICrmWebcallCdrService crmWebcallCdrService;

    /**
     * 查询webCall语音通知列表
     */
//    @PreAuthorize("@ss.hasPermi('crm:webcall:list')")
    @GetMapping("/list")
    public TableDataInfo list(CrmWebcallCdr crmWebcallCdr) {

        List<CrmWebcallCdr> list = crmWebcallCdrService.selectCrmWebcallCdrList(crmWebcallCdr);
        return getDataTable(list);
    }

    /**
     * 导出webCall语音通知列表
     */
    @PreAuthorize("@ss.hasPermi('crm:webcall:export')")
    @Log(title = "webCall语音通知", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CrmWebcallCdr crmWebcallCdr) {
        List<CrmWebcallCdr> list = crmWebcallCdrService.selectCrmWebcallCdrList(crmWebcallCdr);
        ExcelUtil<CrmWebcallCdr> util = new ExcelUtil<CrmWebcallCdr>(CrmWebcallCdr.class);
        util.exportExcel(response, list, "webCall语音通知数据");
    }

    /**
     * 获取webCall语音通知详细信息
     */
    @PreAuthorize("@ss.hasPermi('crm:webcall:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(crmWebcallCdrService.selectCrmWebcallCdrById(id));
    }

    /**
     * 新增webCall语音通知
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody CrmWebcallCdrParamVO crmWebcallCdrParamVO) {
        logger.info("webCall语音通知回掉 crmWebcallCdr ={}", JSONObject.toJSONString(crmWebcallCdrParamVO));
        if (Objects.isNull(crmWebcallCdrParamVO)) {
            return AjaxResult.error("新增WebCall语音通知通话记录 实体参数不能为空");
        }
        return toAjax(crmWebcallCdrService.insertCrmWebcallCdr(buildParam(crmWebcallCdrParamVO)));
    }

    /**
     *  构建参数
     * @param crmWebcallCdrParamVO
     * @return
     */
    private CrmWebcallCdr buildParam(CrmWebcallCdrParamVO crmWebcallCdrParamVO) {
        CrmWebcallCdr crmWebcallCdr = new CrmWebcallCdr();
        crmWebcallCdr.setCdrCallType(Long.valueOf(crmWebcallCdrParamVO.getCdr_call_type()));
        crmWebcallCdr.setCdrEndTime(crmWebcallCdrParamVO.getCdr_end_time());
        crmWebcallCdr.setCdrStatus(Long.valueOf(crmWebcallCdrParamVO.getCdr_status()));
        crmWebcallCdr.setCdrClid(crmWebcallCdrParamVO.getCdr_clid());
        crmWebcallCdr.setCdrCustomerNumber(crmWebcallCdrParamVO.getCdr_customer_number());
        crmWebcallCdr.setCdrEnterpriseId(crmWebcallCdrParamVO.getCdr_enterprise_id());
        crmWebcallCdr.setCdrEndReason(crmWebcallCdrParamVO.getCdr_end_reason());
        crmWebcallCdr.setCdrCustomerNumberType(Long.valueOf(crmWebcallCdrParamVO.getCdr_customer_number_type()));
        crmWebcallCdr.setCdrStartTime(crmWebcallCdrParamVO.getCdr_start_time());
        crmWebcallCdr.setCdrAnswerTime(crmWebcallCdrParamVO.getCdr_answer_time());
        crmWebcallCdr.setCdrCustomerAreaCode(crmWebcallCdrParamVO.getCdr_customer_area_code());
        crmWebcallCdr.setCdrCustomerProvince(crmWebcallCdrParamVO.getCdr_customer_province());
        crmWebcallCdr.setCdrMainUniqueId(crmWebcallCdrParamVO.getCdr_main_unique_id());
        crmWebcallCdr.setCdrRequestUniqueId(crmWebcallCdrParamVO.getCdr_request_unique_id());
        crmWebcallCdr.setCdrCustomerCity(crmWebcallCdrParamVO.getCdr_customer_city());
        crmWebcallCdr.setFileId(crmWebcallCdrParamVO.getFileId());
        return crmWebcallCdr;
    }

    /**
     * 修改webCall语音通知
     */
    @PreAuthorize("@ss.hasPermi('crm:webcall:edit')")
    @Log(title = "webCall语音通知", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CrmWebcallCdr crmWebcallCdr) {
        return toAjax(crmWebcallCdrService.updateCrmWebcallCdr(crmWebcallCdr));
    }

    /**
     * 删除webCall语音通知
     */
    @PreAuthorize("@ss.hasPermi('crm:webcall:remove')")
    @Log(title = "webCall语音通知", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
            return toAjax(crmWebcallCdrService.deleteCrmWebcallCdrByIds(ids));
    }
}
